const knex = require('../../models/knex');
const Category = require('../../models/blog_category');
const Article = require('../../models/blog_article');
const ArticleCategory = require('../../models/article_category_tags');

const ControllerCategory = {
    getList: async function (req, res, next) {
        try {
            const articles = Article.table;
            const or = ArticleCategory.table;
            const list = await Category.all();
            const arr = await knex(or)
                .join(articles, function () {
                    this.on(`${articles}.id`, '=', `${or}.article_id`)
                        .andOn(`${articles}.status`, '=', 1)
                })
                .select(
                    `${or}.id`, `${or}.category_id`
                );


            list.forEach((i) => {
                i.num = 0;
                arr.forEach((v) => {
                    if (i.id == v.category_id) i.num += 1
                })
            })


            res.json({ code: 200, msg: '连接成功', data: { list } })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerCategory;